﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MOD;
using DAL;

namespace BLL
{
    public class B_AdRepost
    {
        private int iDataConn = DataBase.IN_S2;
        public int Insert(M_AdRepost mAr)
        {
            DataBase dDb = new DataBase();
            StringBuilder sCmdText = new StringBuilder();

            sCmdText.Append("Insert into ad_repost ");
            sCmdText.Append("(AdId,RepostUserId,[RepostTime] )");
            sCmdText.Append("values");
            sCmdText.Append("(@AdId,@RepostUserId,@RepostTime)");

            SqlParameter[] sSp = {
                                   new SqlParameter("@AdId",SqlDbType.BigInt),
                                   new SqlParameter("@RepostUserId",SqlDbType.BigInt),
                                   new SqlParameter("@RepostTime",SqlDbType.DateTime)           
                                };
            sSp[0].Value = mAr.AdId;
            sSp[1].Value = mAr.RepostUserId;
            sSp[2].Value = mAr.RepostTime;

            return dDb.ExecuteCmd(sCmdText.ToString(), iDataConn, sSp);
        }
        public DataTable SelectRepost(Int64 iUserId)
        {
            DataBase dDb = new DataBase();
            StringBuilder sCmdText = new StringBuilder();

            sCmdText.Append("Select count(*) as count from ad_repost ");
            sCmdText.Append("where RepostUserId = @UserId  ");
            SqlParameter[] sSp = {
                                    new SqlParameter("@UserId",SqlDbType.BigInt)
                                 };
            sSp[0].Value = iUserId;

            return dDb.GetDataTable(sCmdText.ToString(), iDataConn, sSp);
        }
        public DataTable SelectRepostUserCount(Int64 iUserId)
        {
            DataBase dDb = new DataBase();
            StringBuilder sCmdText = new StringBuilder();

            sCmdText.Append("Select count(distinct ad_repost.RepostUserid) s1 from ad_info ");
            sCmdText.Append("inner join ad_repost on ad_info.WeiboId = ad_repost.Adid ");
            sCmdText.Append("where ad_info.userid = @UserId");
            SqlParameter[] sSp = {
                                    new SqlParameter("@UserId",SqlDbType.BigInt)
                                 };
            sSp[0].Value = iUserId;

            return dDb.GetDataTable(sCmdText.ToString(), iDataConn, sSp);
        }
        public DataTable SelectUserRepostCount(Int64 iUserId)
        {
            DataBase dDb = new DataBase();
            StringBuilder sCmdText = new StringBuilder();

            sCmdText.Append("Select count(distinct ad_info.Userid) s1 from ad_repost  ");
            sCmdText.Append("inner join ad_info on ad_repost.Adid = ad_info.WeiboId ");
            sCmdText.Append("where ad_repost.RepostUserId = @UserId");
            SqlParameter[] sSp = {
                                    new SqlParameter("@UserId",SqlDbType.BigInt)
                                 };
            sSp[0].Value = iUserId;

            return dDb.GetDataTable(sCmdText.ToString(), iDataConn, sSp);
        }
    }
}
